package com.yvdedu.hospital.controller;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yvdedu.hospital.entity.Login;
import com.yvdedu.hospital.entity.Role;
import com.yvdedu.hospital.entity.User;
import com.yvdedu.hospital.service.DoctorService;
import com.yvdedu.hospital.service.HospitalAdminService;
import com.yvdedu.hospital.service.LoginService;


@Controller
@RequestMapping("user/doctor")
public class DoctorController {
	
	@Resource
	DoctorService  doctorService;
	
	@Resource
	LoginService loginService;
	List<User> user;
	List<Login> login;
	List<Role> role;
	
//	添加
	@RequestMapping("add")
	public void add(){
	}
	
//	执行添加
	@RequestMapping("doadd")
	public Object doadd( Login login, User user, Model model){
		
//		System.out.println(login);
//		System.out.println(doctor);
		
//		判断登录账号是否存在
		Login lo = loginService.loginquery(login);
//		System.out.println("------------------------------------------------list="+lo);
		if ( lo!=null ){
			model.addAttribute("data","登录名已存在！");
			model.addAttribute("acc", "false");
			model.addAttribute("url", "add");
			return "info/tips";
//			model.addAttribute("data","登录名已存在！");
//			return "user/doctor/add";
		}else {
			
			//得到long类型当前时间
			long ltime = System.currentTimeMillis();
			//new日期对象
			Date date = new Date(ltime);
			//转换提日期输出格式
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			String time = dateFormat.format(date);
			login.setCreateDate(time);
			
//			添加Login
			loginService.loginadd(login);
			
//			拿到添加的Login的id
			Login s = loginService.loginquery(login);
			user.setId(s.getId());
			
//			添加User
			doctorService.doctoradd(user);
			model.addAttribute("acc", "true");
			model.addAttribute("url", "list");
			return "info/tips";
//			return "redirect:list";
		}
	}
	
//	列表
	@RequestMapping("list")
	public void list( Model model ){
		user = doctorService.doctorquery(4);
//		System.out.println(user);
		model.addAttribute("doctorList", user);
		
	}
	
//	查询
	@RequestMapping("search")
	public Object search( User doctor,String userName, Model model ){
		System.out.println(doctor);
		user = doctorService.doctorNamequery(doctor);
		doctor.setUserName(userName);
//		System.out.println("******************************************"+user);
		model.addAttribute("doctorList", user);
		return "user/doctor/list";
		
	}
	
//	编辑医生信息
	@RequestMapping("edit")
	public void edit(){
		
	}
	
//	执行编辑
	@RequestMapping("doedit")
	public Object doedit(User user,Model model ){
		System.out.println(user);
		doctorService.doctorUpdate(user);
		
		model.addAttribute("acc", "true");
		model.addAttribute("url", "list");
		return "info/tips";
//		return "redirect:list";
	}
	

//	删除
	@RequestMapping("delete")
	public Object delete( int id, Model model ){
		doctorService.doctordelete(id);
		model.addAttribute("acc", "true");
		model.addAttribute("url", "list");
		return "info/tips";
	}
	
}
